﻿using NLog;
using NLog.Fluent;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonNet48.LogUtils.NLog
{
    /// <summary>
    /// NLog日志操作类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class NLog
    {
        /// <summary>
        /// NLog配置文件路径（可根据实际情况修改）
        /// </summary>
        private static readonly string _ConfigFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "NLog.cfg.xml");

        /// <summary>
        /// NLog日志操作对象
        /// </summary>
        private readonly Logger _Logger;  //LogManager.GetCurrentClassLogger(typeof(T));

        /// <summary>
        /// 构造
        /// </summary>
        public NLog()
        {
            _Logger = LogManager.LoadConfiguration(_ConfigFilePath).GetCurrentClassLogger();
        }

        /// <summary>
        /// 异常日志
        /// </summary>
        /// <param name="msg">异常输出信息</param>
        /// <param name="ex">异常对象</param>
        public void Error(string msg, Exception ex)
        {
            Console.WriteLine(msg);
            _Logger.Error(ex, msg);
        }

        /// <summary>
        /// 告警日志
        /// </summary>
        /// <param name="msg">告警输出信息</param>
        /// <param name="ex">异常对象，没有异常可以不传，默认为null</param>
        public void Warn(string msg, Exception ex = null)
        {
            Console.WriteLine(msg);
            _Logger.Warn(ex, msg);
        }

        /// <summary>
        /// 信息日志
        /// </summary>
        /// <param name="msg">提示输出信息</param>
        /// <param name="ex">异常对象，没有异常可以不传，默认为null</param>
        public void Info(string msg, Exception ex = null)
        {
            Console.WriteLine(msg, ex);
            _Logger.Info(ex, msg);
        }

        /// <summary>
        /// 调试日志
        /// </summary>
        /// <param name="msg">调试输出信息</param>
        /// <param name="ex">异常对象，没有异常可以不传，默认为null</param>
        public void Debug(string msg, Exception ex = null)
        {
            Console.WriteLine(msg, ex);
            _Logger.Debug(ex, msg);
        }
    }
}
